﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Gav.Components.MapLayers;
using Gav.Components;
using Gav.Components.Internal;
using Gav.Data;

namespace Infovizprojekt
{
    public class PCHandler : BaseHandler
    {
        public ParallelCoordinatesPlot _pcPlotVariables = new ParallelCoordinatesPlot();
        public ParallelCoordinatesPlot _pcPlotYears = new ParallelCoordinatesPlot();
        Gav.Management.GavToolTip _gavToolTip;
        Gav.Graphics.MouseHoverController _hoverControllerVar;
        Gav.Graphics.MouseHoverController _hoverControllerYear;

        public PCHandler(MainWindow mainWindow) : base(mainWindow)
        {
            init();
        }

        public void init()
        {
            //Manage the parallel coordinates plots
            _pcPlotVariables.Input = _mainWindow.DataHandler.MainDataCubeProvider;
            _pcPlotVariables.Headers = _mainWindow.DataHandler.getColumnHeaders();
            _pcPlotVariables.HeaderLayer.SelectedHeadersList.Add(MainWindow.PC_VAR_PLOT_INIT_VALUE);

            _pcPlotYears.Input = _mainWindow.DataHandler.YearDataCubeProvider;
            _pcPlotYears.Headers = _mainWindow.DataHandler.getYearsAsList();
            _mainWindow.DataHandler.YearDataCubeProvider.VarIndex = MainWindow.PC_YEARS_PLOT_INIT_VALUE;
            YearTransformer_Changed(new object(), new EventArgs());//fullösning för att få axlarna att skala ordentligt
              
            _pcPlotVariables.ColorMap = _mainWindow.mainColorMap;
            _pcPlotYears.ColorMap = _mainWindow.mainColorMap;
            

            _mainWindow.yearSlider.Minimum = 0;
            _mainWindow.yearSlider.Maximum = DataHandler.EndYear - DataHandler.StartYear;
            _mainWindow.yearSlider.TickFrequency = 1;
            _mainWindow.yearSlider.Value = 0;
            //_mainWindow.yearSlider.SmallChange = 1;
            //_mainWindow.yearSlider.LargeChange = 5;

            // listeners
            _mainWindow.DataHandler.YearDataCubeProvider.Changed += new EventHandler(YearTransformer_Changed);
            _pcPlotVariables.HeaderClicked += new EventHandler<Gav.Event.IndexClickedEventArgs>(_pcPlotVariables_HeaderClicked);
            _pcPlotVariables.FilterChanged += new EventHandler(_pcPlotVariables_FilterChanged);

            _mainWindow.ViewManager.Add(_pcPlotVariables, _mainWindow.getPCPanelVariables());
            _mainWindow.ViewManager.Add(_pcPlotYears, _mainWindow.getPCPanelYears());


            _mainWindow.yearSlider.ValueChanged += new EventHandler(yearSlider_ValueChanged);

            _mainWindow.sapManager.AddComponent(_pcPlotVariables);
            _mainWindow.sapManager.AddComponent(_pcPlotYears);

            // add tooltip to pc plt
            _gavToolTip = new Gav.Management.GavToolTip(_mainWindow);
            _gavToolTip.FadeTime = 150;
            //listeners FOR HOVER controller
            _hoverControllerVar = new Gav.Graphics.MouseHoverController(_mainWindow.getPCPanelVariables(), 1, 50);
            _hoverControllerYear = new Gav.Graphics.MouseHoverController(_mainWindow.getPCPanelYears() , 1, 50);
            _hoverControllerVar.Hover += new EventHandler(_hoverController_Hover_Var);
            _hoverControllerYear.Hover += new EventHandler(_hoverController_Hover_Year);
            _hoverControllerVar.HoverEnd += new EventHandler(_hoverController_HoverEnd_Var);
            _hoverControllerYear.HoverEnd += new EventHandler(_hoverController_HoverEnd_Year);
            // dont know why I´m doing this but it makes the tooltip show at correct location the first time its called
            _gavToolTip.Show(new System.Drawing.Point(1,1));
            _gavToolTip.Hide();
            
        }

        //make sure that all windows updates when filtering
        void _pcPlotVariables_FilterChanged(object sender, EventArgs e)
        {
            _mainWindow.PlotHandler._scatterplot.Invalidate();
        }

        void _hoverController_HoverEnd_Var(object sender, EventArgs e)
        {
            _gavToolTip.Hide();
        }
        void _hoverController_HoverEnd_Year(object sender, EventArgs e)
        {
            _gavToolTip.Hide();
        }

        //show tooltip when hovering over line in Variable plot
        void _hoverController_Hover_Var(object sender, EventArgs e)
        {
            //get country id
            int[] countryList = _pcPlotVariables.GetLineIndexesAtPosition(_hoverControllerVar.HoverPosition, 2).ToArray();
            //check if country is hidden
            int country = -1;
            if (countryList.Length > 0)
            {
                for (int i = 0; i < countryList.Length; i++)
                {
                    if (_pcPlotVariables.IndexVisibilityManager.GetVisibility(countryList[i]))
                    {
                        country = countryList[i];
                        break;
                    }
                }
            }
            if (country > -1)
            {
                //init tooltip
                String tooltip = _mainWindow.DataHandler.RawDataProvider.NaNColumns[0][0][country].ToString();

                // where to draw tooltip
                //System.Drawing.Point atThisLocation = new System.Drawing.Point(_hoverControllerVar.HoverScreenPosition.X - 3, _hoverControllerVar.HoverScreenPosition.Y - 3);
                
                //get current year
                int year = _mainWindow.yearSlider.Value;
                //loop over info to show in tooltip
                for (int i = 0; i < _mainWindow.DataHandler.RawDataProvider.ColumnHeaders.Count; i++)
                {
                    tooltip += "\n" + _mainWindow.DataHandler.RawDataProvider.ColumnHeaders[i];
                    tooltip += "   " + _mainWindow.DataHandler.getValueFromDataCube(i, country, year);
                }
                _gavToolTip.Text = tooltip;
                _gavToolTip.Show(_hoverControllerVar.HoverScreenPosition);
            }
            
        }
        //show tooltip when hovering over line in Years plot
        void _hoverController_Hover_Year(object sender, EventArgs e)
        {

            //get country id
            int[] countryList = _pcPlotYears.GetLineIndexesAtPosition(_hoverControllerYear.HoverPosition, 2).ToArray();
            //check if country is hidden
            int country = -1;
            if (countryList.Length > 0)
            {
                for (int i = 0; i < countryList.Length; i++)
                {
                    if (_pcPlotYears.IndexVisibilityManager.GetVisibility(countryList[i]))
                    {
                        country = countryList[i];
                        break;
                    }
                }
            }
            if (country > -1)
            {
                //init tooltip
                String tooltip = _mainWindow.DataHandler.RawDataProvider.NaNColumns[0][0][country].ToString();
                // if no header has been selected set to zero
                int selectedHeader;
                if (_pcPlotVariables.HeaderLayer.SelectedHeadersList.Count > 0)
                {
                    selectedHeader = _pcPlotVariables.HeaderLayer.SelectedHeadersList[0];
                }
                else
                {
                    selectedHeader = 0;
                }
                // add header to tooltip
                tooltip += ":  " + _mainWindow.DataHandler.RawDataProvider.ColumnHeaders[selectedHeader];
                //loop over all years for the selected variable
                for (int year = 0; year < _mainWindow.DataHandler.RawDataProvider.GetDataCube().DataArray.GetLength(2); year++)
                {   
                    tooltip += "\n" + (year + 1990) + "   " + _mainWindow.DataHandler.getValueFromDataCube(selectedHeader, country, year);
                }
                _gavToolTip.Text = tooltip;
                _gavToolTip.Show(_hoverControllerYear.HoverScreenPosition);
            }
        }

        void _pcPlotVariables_HeaderClicked(object sender, Gav.Event.IndexClickedEventArgs e)
        {
            _mainWindow.DataHandler.YearDataCubeProvider.VarIndex = e.IndexClicked;
            _mainWindow.DataHandler.YearDataCubeProvider.CommitChanges();

            _mainWindow.mainColorMap.Index = e.IndexClicked;
            _mainWindow.mainColorMap.Invalidate();

            _pcPlotVariables.HeaderLayer.SelectedHeadersList.Clear();
            _pcPlotVariables.HeaderLayer.SelectedHeadersList.Add(e.IndexClicked);

            
            _mainWindow.ViewManager.InvalidateAll();
        }

        void yearSlider_ValueChanged(object sender, EventArgs e)
        {
            _mainWindow.DataHandler.MainDataCubeProvider.Slice = (int)_mainWindow.yearSlider.Value;
            _mainWindow.DataHandler.MainDataCubeProvider.CommitChanges();
            
            int temp;
            temp = _mainWindow.yearSlider.Value + 1990;
            _mainWindow.selectedYearLabel.Text = temp.ToString();
            _mainWindow.ViewManager.InvalidateAll();
        }

        void YearTransformer_Changed(object sender, EventArgs e)
        {
            List<float> minList = new List<float>();
            List<float> maxList = new List<float>();

            _mainWindow.DataHandler.YearDataCubeProvider.GetDataCube().GetAllColumnMaxMin(out maxList, out minList);

            float min = minList.Min();
            float max = maxList.Max();

            for (int i = 0; i < _mainWindow.DataHandler.YearDataCubeProvider.GetDataCube().GetAxisLength(Axis.X); i++)
            {
                _pcPlotYears.SetAxisMaxMin(i, max, min);
            }

        }


      

    }
}
